package com.qxsx.midi.activity.me;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.activity.main.MainActivity;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.utils.Base64Coder;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.view.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;

/**
 * Created by Joney on 2015/9/1.
 * 填写资料
 */
public class InformationActivity extends BaseActivity implements View.OnClickListener {
    private CustomProgress customProgress;

    private EditText et_nickname, et_email, et_name, et_idcard;//昵称，电子邮箱，姓名，身份证号
    private CircleImageView iv_head;
    private Button btn_finish;


    private static final int IMAGE_REQUEST_CODE = 0;
    private static final int RESULT_REQUEST_CODE = 2;
    private Intent dataIntent = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_information);
        setTitle("填写信息");
        setNavBtn(R.mipmap.back, "", 0, "跳过");
        findById();

    }

    private void findById() {
        et_nickname = (EditText) findViewById(R.id.et_nickname);
        et_email = (EditText) findViewById(R.id.et_email);
        et_name = (EditText) findViewById(R.id.et_name);
        et_idcard = (EditText) findViewById(R.id.et_idcard);
        btn_finish = (Button) findViewById(R.id.btn_finish);
        btn_finish.setOnClickListener(this);
        iv_head = (CircleImageView) findViewById(R.id.iv_head);
        iv_head.setOnClickListener(this);


    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {

        switch (component) {
            case LEFT:
                AppManager.getAppManager().finishActivity();
                finish();
                break;
            case RIGHT:
                AppManager.getAppManager().finishActivity();
                finish();
                break;
        }
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.iv_head:
                Intent intentFromGallery = new Intent();
                intentFromGallery.setType("image/*"); // 设置文件类型
                intentFromGallery
                        .setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intentFromGallery,
                        IMAGE_REQUEST_CODE);
                break;
            case R.id.btn_finish:
                register();

                break;
            default:
                break;
        }
    }

    /**
     * 裁剪图片方法实现
     *
     * @param uri
     */
    public void startPhotoZoom(Uri uri) {

        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // 设置裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("return-data", true);
        startActivityForResult(intent, 2);
    }

    /**
     * 保存裁剪之后的图片数据
     *
     * @param data
     */
    @SuppressWarnings("deprecation")
    private void getImageToView(Intent data) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            Drawable drawable = new BitmapDrawable(photo);

            iv_head.setImageDrawable(drawable);
        }

    }


    /**
     * 将拿到的上传
     */
    private void register() {
        customProgress = CustomProgress.show(InformationActivity.this, "上传中....", true, null);
        JSONArray array = new JSONArray();
        final JSONObject object = new JSONObject();
        try {
            String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
            Log.w("user_id", user_id);
            object.put("user_id", user_id);
            //头像
            if (dataIntent != null) {
                Bundle extras = dataIntent.getExtras();
                if (extras != null) {
                    Bitmap photo = extras.getParcelable("data");
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 60, stream);
                    byte[] bytes = stream.toByteArray();
                    // 将图片流以字符串形式存储下来
                    final String picStr = new String(Base64Coder.encodeLines(bytes));
                    object.put("head_img", picStr);
                }
            }
            //昵称
            if (!TextUtils.isEmpty(et_nickname.getText().toString())) {
                object.put("nickname", et_nickname.getText().toString());
            }
            //邮箱
            if (!TextUtils.isEmpty(et_email.getText().toString())) {
                object.put("eamil", et_email.getText().toString());
            }
            //真是姓名
            if (!TextUtils.isEmpty(et_name.getText().toString())) {
                object.put("real_name", et_name.getText().toString());
            }
            //身份证号
            if (!TextUtils.isEmpty(et_idcard.getText().toString())) {
                object.put("id_card", et_idcard.getText().toString());
            }
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("106", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                AppManager.getAppManager().finishActivity();
                finish();
                customProgress.dismiss();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_CANCELED) {

            switch (requestCode) {
                case IMAGE_REQUEST_CODE:
                    startPhotoZoom(data.getData());

                    break;
                case RESULT_REQUEST_CODE:
                    if (data != null) {
                        dataIntent = data;
                        getImageToView(data);
                    }
                    break;
            }

        }

    }

}
